// 护理异常页面的数据
var data = [
    {location:'医技科室',code:'HLY20180319102942699',department:'新生儿小婴儿科',escalation:'2022-06-16',closure:'2022-06-16',status:'提交'},
    {location:'急诊',code:'GRB20180319102942678',department:'血液科',escalation:'2022-06-16',closure:'2022-06-17',status:'终止'},
    {location:'门诊',code:'HLY20180319102942699',department:'血液科',escalation:'2022-06-17',closure:'2022-06-18',status:'科室审批'},
    {location:'手术麻醉',code:'HLY20180319102942699',department:'血液科',escalation:'2022-06-18',closure:'2022-06-18',status:'创建'},
    {location:'住院',code:'GRB20180319102942678',department:'新生儿小婴儿科',escalation:'2022-06-19',closure:'2022-06-19',status:'终止'},
    {location:'手术麻醉',code:'HLY20180319102942699',department:'康复科',escalation:'2022-06-19',closure:'2022-06-21',status:'上报存档'},
    {location:'产房',code:'HLY20180319102942699',department:'肾脏免疫科',escalation:'2022-06-20',closure:'2022-06-20',status:'终止'},
    {location:'其他场所',code:'HLY20180319102942699',department:'血液科',escalation:'2022-06-21',closure:'2022-06-23',status:'提交'},
    {location:'住院',code:'GRB20180319102942678',department:'肾脏免疫科',escalation:'2022-06-21',closure:'2022-06-23',status:'科室审批'},
    {location:'场所不明',code:'HLY20180319102942699',department:'康复科',escalation:'2022-06-22',closure:'2022-06-23',status:'提交'},
    {location:'医技科室',code:'HLY20180319102942699',department:'血液科',escalation:'2022-08-01',closure:'2022-08-06',status:'创建'},
    {location:'住院',code:'GRB20180319102942678',department:'新生儿小婴儿科',escalation:'2022-06-16',closure:'2022-06-26',status:'科室审批'},
    {location:'产房',code:'HLY20180319102942699',department:'血液科',escalation:'2022-07-17',closure:'2022-07-21',status:'上报存档'},
    {location:'门诊',code:'HLY20180319102942699',department:'肾脏免疫科',escalation:'2022-08-02',closure:'2022-08-08',status:'提交'},
    {location:'公共活动区',code:'HLY20180319102942699',department:'血液科',escalation:'2022-07-16',closure:'2022-07-17',status:'创建'},
    {location:'急诊',code:'GRB20180319102942678',department:'血液科',escalation:'2022-08-03',closure:'2022-08-16',status:'终止'},
    {location:'手术麻醉',code:'HLY20180319102942699',department:'康复科',escalation:'2022-08-03',closure:'2022-08-16',status:'科室审批'},
    {location:'住院',code:'HLY20180319102942699',department:'康复科',escalation:'2022-08-03',closure:'2022-08-11',status:'创建'},
    {location:'其他场所',code:'GRB20180319102942678',department:'康复科',escalation:'2022-08-08',closure:'2022-08-16',status:'终止'},
    {location:'医技科室',code:'HLY20180319102942699',department:'肾脏免疫科',escalation:'2022-07-16',closure:'2022-08-16',status:'上报存档'},
    {location:'其他场所',code:'GRB20180319102942678',department:'新生儿小婴儿科',escalation:'2022-08-03',closure:'2022-08-03',status:'创建'},
    {location:'门诊',code:'GRB20180319102942678',department:'肾脏免疫科',escalation:'2022-07-23',closure:'2022-07-24',status:'上报存档'},
    {location:'场所不明',code:'HLY20180319102942699',department:'血液科',escalation:'2022-07-20',closure:'2022-07-23',status:'提交'},
    {location:'住院',code:'GRB20180319102942678',department:'肾脏免疫科',escalation:'2022-07-21',closure:'2022-07-23',status:'科室审批'},
    {location:'场所不明',code:'HLY20180319102942699',department:'康复科',escalation:'2022-07-22',closure:'2022-07-23',status:'提交'},
    {location:'医技科室',code:'HLY20180319102942699',department:'血液科',escalation:'2022-08-01',closure:'2022-08-06',status:'创建'},
    {location:'住院',code:'GRB20180319102942678',department:'新生儿小婴儿科',escalation:'2022-07-16',closure:'2022-07-26',status:'科室审批'},
    {location:'产房',code:'HLY20180319102942699',department:'血液科',escalation:'2022-08-17',closure:'2022-08-21',status:'上报存档'},
    {location:'门诊',code:'HLY20180319102942699',department:'肾脏免疫科',escalation:'2022-07-02',closure:'2022-07-07',status:'提交'},
    {location:'公共活动区',code:'HLY20180319102942699',department:'血液科',escalation:'2022-08-16',closure:'2022-08-17',status:'创建'},
    {location:'急诊',code:'GRB20180319102942678',department:'血液科',escalation:'2022-07-03',closure:'2022-07-16',status:'终止'},
    {location:'手术麻醉',code:'HLY20180319102942699',department:'康复科',escalation:'2022-07-03',closure:'2022-07-16',status:'科室审批'},
    {location:'住院',code:'HLY20180319102942699',department:'康复科',escalation:'2022-07-03',closure:'2022-07-11',status:'创建'},
    {location:'其他场所',code:'GRB20180319102942678',department:'康复科',escalation:'2022-07-07',closure:'2022-07-16',status:'终止'},
    {location:'医技科室',code:'HLY20180319102942699',department:'肾脏免疫科',escalation:'2022-08-16',closure:'2022-07-16',status:'上报存档'},
    {location:'其他场所',code:'GRB20180319102942678',department:'新生儿小婴儿科',escalation:'2022-07-03',closure:'2022-08-23',status:'创建'},
    {location:'门诊',code:'GRB20180319102942678',department:'肾脏免疫科',escalation:'2022-08-23',closure:'2022-08-24',status:'上报存档'},
    {location:'场所不明',code:'HLY20180319102942699',department:'血液科',escalation:'2022-08-20',closure:'2022-08-23',status:'提交'}
];

//当前的页码
var page = 1;
//每页的条数
var count = 15;
//获取tbody
var oTBody = document.querySelector('tbody');
//获取分页容器
var oPagination = document.querySelector('.pagination');
//获取上一页的按钮
var oPrevBtn = document.querySelector('.prev')
//获取下一页的按钮
var oNextBtn = document.querySelector('.next')
//获取搜索的输入框和选择框
var oSearchInp = document.querySelector('.searchInp');
var oChose1 = document.querySelector('.chose1');
var oChose2 = document.querySelector('.chose2');
var oChose3 = document.querySelector('.chose3');
var oDate = document.querySelector('.date');
var oDate2 = document.querySelector('.date2');
//获取搜索查询按钮
var oSearchBtn = document.querySelector('.searchBtn');
// 获取清除innerHTML内容的按钮
var oDelBtn=document.querySelector('.del');
// 获取修改状态的按钮
var oEditBtn=document.querySelector('.editStatus');
// 获取事件追踪按钮
var oEventBtn=document.querySelector('.eventTrace');
// 获取第一页按钮
var oFirBtn=document.querySelector('.first');


//页面初始化的渲染功能
function render() {
    //查看搜索框的内容
    var str1 = oSearchInp.value;//搜索的内容
    var str2 = oChose1.value;
    var str3 = oChose2.value;
    var str4 = oChose3.value;
    var str5= oDate.value==''?new Date('2002-8-25').getTime():new Date(oDate.value).getTime(); //开始时间
    var str6= oDate2.value==''?new Date().getTime():new Date(oDate2.value).getTime(); //结束时间
    var searchData = [];//搜索后的数据
    var data1=[];
    var data2=[];
    var data3=[];
    var data4=[];

    if(str1.trim()==''){
        data1 = data;
    }else{
        data1 = data.filter(function(v){
            return v.code.includes(str1)
        })
    }
    console.log('1',data1)

    // 第2个搜索条件框
    if(str2.includes('---')){
        
        data2 = data1;
    }else{
        data2 = data1.filter(function(v){
            return v.department.includes(str2)
        })
    }
    console.log('2',data2)
console.log(str2.includes('-'))
    // 第3个搜索条件框
    if(str3.includes('-')){
        data3 = data2;
    }else{
        data3 = data2.filter(function(v){
            return v.location.includes(str3)
        })
    }
    console.log('3',data3)

    // 第4个搜索条件框
    if(str4.includes('-')){
        data4 = data3;
    }else{
        data4 = data3.filter(function(v){
            return v.status.includes(str4)
        })
    }
    console.log('4',data4)



    // 第5个搜索条件框
   searchData = data4.filter(function(v){
        var time=new Date(v.escalation).getTime();
        return time>=str5 && time<=str6
    })
    
    console.log('5',searchData)

    //让table为空
    oTBody.innerHTML = '';
    //根据count和page准备要渲染的数据
    var renderArr = searchData.slice((page - 1) * count, page * count);
    //循环要渲染的数据
    for (var i = 0; i < renderArr.length; i++) {
        //创建tr
        var oTr = document.createElement('tr');
        //给tr创建内容
        oTr.innerHTML = `
        <td><input type="checkbox" name="list"></td>
        <td>${renderArr[i].code}</td>
        <td>${renderArr[i].status}</td>
        <td>${renderArr[i].department}</td>
        <td>${renderArr[i].escalation}</td>
        <td>${renderArr[i].closure}</td>
        <td>${renderArr[i].location}</td>
    `;
        //把tr创建到tbody里面
        oTBody.appendChild(oTr);
    }

    

    //渲染页码
    //找到所有的1，2，3，4的页码
    var aPageBtn = document.querySelectorAll('.pageBtn');
    //删除所有的页码
    for (var i = 0; i < aPageBtn.length; i++) {
        oPagination.removeChild(aPageBtn[i].parentNode);
    }
    //根据数据的长度重新创建页码
    for (var i = 1; i <= Math.ceil(searchData.length / count); i++) {
        //创建li
        var oLi = document.createElement('li');
        //给li创建内容
        oLi.innerHTML = `<a class="pageBtn" href="#">${i}</a>`;
        //如果li和当前的页码一致，给他加一个选中的class
        if (i == page) {
            oLi.classList.add('active');
        }
        //在下一页的前面插入页码
        oPagination.insertBefore(oLi, oPagination.lastElementChild);
    }
}


//一上来就渲染一次数据
render();
//给分页加事件
oPagination.addEventListener('click', changePage);
function changePage() {
    //如果当前点击的按钮有pageBtn这个class名字
    if (event.target.className == 'pageBtn') {
        //修改全局的page
        page = event.target.innerText;
        //渲染table，渲染分页
        render();
    }
}
//给第一页、上一页、下一页加事件
oPrevBtn.addEventListener('click', prev);
oNextBtn.addEventListener('click', next);
oFirBtn.addEventListener('click',firstPage)

function prev() {
    if (page > 1) {
        page--;
        render();
    }
}
function next() {
    if (page < Math.ceil(data.length / count)) {
        page++;
        render();
    }
}
function firstPage(){
    page=1
    render();
}

//搜索
oSearchBtn.addEventListener('click', searchFn);
function searchFn() {
    page = 1;
    render();
}

oDelBtn.addEventListener('click',delFn);
function delFn(){
    oSearchInp.value='';
    oChose1.value='---请选择---';
    oChose2.value='---请选择---';
    oChose3.value='---请选择---';
    oDate.value='';
    oDate2.value='';
    searchData=data;
    render();
}


oEditBtn.addEventListener('click', editStatus);
function editStatus() {
        $('#editStatus').modal('show');
}
oEventBtn.addEventListener('click', eventTrace);
function eventTrace() {
        $('#eventTrace').modal('show');
}












